package com.hlmc.spider;

import com.hlmc.entity.Yun_Video;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

@Service("yun_spider")
public class YunVideo_spider  extends BaseProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100).setCharset("utf-8" + "");

    @Override
    public void process(Page page) {

       if(page.getUrl().toString().contains("www.caijizy.com/?m=vod-type-id-16-pg-"))
       {
           page.addTargetRequests(page.getHtml().xpath("//tr/td[1]/a/@href").all());
           page.addTargetRequests(page.getHtml().css("a.pagelink_a","href").all());
       }
        if(page.getUrl().toString().contains("www.caijizy.com/?m=vod-detail")){
           String image=page.getHtml().xpath("//div[@class='videoPic']/img/@src").toString();
           String title=page.getHtml().xpath("//div[@class='videoDetail']/li[1]/text()").toString().replace("影片名称：","");
           String url=page.getHtml().xpath("//div[@class='movievod']/ul/li[4]/text()").toString();

           if( (image == null)||(image.replace(" ", "").equals(""))){

               page.setSkip(true);
           }

            if (url != null&&!url.replace(" ", "").equals("")){
                page.putField("image",image);
                page.putField("title",title);
                page.putField("url",url);
            }

       }

    }
    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {

        // private  DS_pipeline ds_pipeline;
        Spider.create(new YunVideo_spider()).addUrl("http://www.caijizy.com/?m=vod-type-id-16-pg-1.html")
                .addPipeline(new yun_pipeline())
                .thread(5).run();

    }
}
